﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

using ATMCommonLib;

namespace ATMCommon.ATMConfig
{
    public class ATMConfigXML : ATMConfigBase
    {
        private string mConfigPath = string.Empty;
        private XmlDocument xmlDoc = null;

        private bool LoadATMConfig(string configPath)
        {
            try
            {
                mConfigPath = configPath;
                xmlDoc.Load(configPath);

                return true;
            }
            catch (Exception ex)
            {
                ATMCommonLogger.Instance.Error(string.Format("ATMConfigHelper.LoadATMConfig() = {0}", ex.Message));
                return false;
            }
        }

        #region //////////////////// XML HELPER ////////////////////

        private bool GetXMLValue(string path, string eleName, out string eleValue)
        {
            eleValue = string.Empty;

            try
            {
                path += "/ConfigItem";
                XmlNodeList nodeList = xmlDoc.SelectNodes(path);

                foreach (XmlNode node in nodeList)
                {
                    XmlElement xmlEle = node as XmlElement;

                    if (xmlEle != null)
                    {
                        if (xmlEle.GetAttribute("Name").Equals(eleName, StringComparison.OrdinalIgnoreCase))
                        {
                            eleValue = xmlEle.GetAttribute("Value");
                        }
                    }
                }

                return true;
            }
            catch (Exception)
            {
                
            }

            return false;
        }

        #endregion

        #region ////////////////////  PUBLIC IMPL //////////////////

        public override string GetStringValue(LATMConfigPath path, string key, string defaultValue)
        {
            bool retBool = false;
            string retValue = string.Empty;

            try
            {
                if (GetXMLValue(LATMConfigDic[path], key, out retValue))
                {
                    retBool = true;
                }
            }
            catch (Exception)
            {
                retBool = false;
            }


            if (!retBool)
            {
                ATMCommonLogger.Instance.Info(string.Format("Failed Get [{0}] [{1}], User Default Value [{2}]", LATMConfigDic[path], key, defaultValue));
                retValue = defaultValue;
            }

            return retValue;
        }

        public override int GetIntValue(LATMConfigPath path, string key, int defaultValue)
        {
            bool retBool = false;
            int retValue = -1;
            string retStr =string.Empty;

            try
            {
                if (GetXMLValue(LATMConfigDic[path], key, out retStr))
                {
                    if (int.TryParse(retStr, out retValue))
                    {
                        retBool = true;
                    }
                }
            }
            catch (Exception)
            {
                retBool = false;
            }

            if (!retBool)
            {
                ATMCommonLogger.Instance.Info(string.Format("Failed Get [{0}] [{1}], User Default Value [{2}]", LATMConfigDic[path], key, defaultValue));
                retValue = defaultValue;
            }

            return retValue;
        }

        public override int GetIntValue(LATMConfigPath path, string key, int defaultValue, int min, int max)
        {
            bool retBool = false;
            int retValue = -1;
            string retStr = string.Empty;

            try
            {
                if (GetXMLValue(LATMConfigDic[path], key, out retStr))
                {
                    if (int.TryParse(retStr, out retValue))
                    {
                        if (retValue >= min && retValue <= max)
                        {
                            retBool = true;
                        }
                    }
                }
            }
            catch (Exception)
            {
                retBool = false;
            }

            if (!retBool)
            {
                ATMCommonLogger.Instance.Info(string.Format("Failed Get [{0}] [{1}], User Default Value [{2}]", LATMConfigDic[path], key, defaultValue));
                retValue = defaultValue;
            }

            return retValue;
        }

        public override bool Init()
        {

            LATMConfigDic.Add(LATMConfigPath.Config, "LittleATM/Config");
            LATMConfigDic.Add(LATMConfigPath.DeviceManager, "LittleATM/DeviceManager");
            LATMConfigDic.Add(LATMConfigPath.Hardware, "LittleATM/Hardware");
            LATMConfigDic.Add(LATMConfigPath.ModeManager, "LittleATM/ModeManager");

            return LoadATMConfig(ATMCommonLib.StaticVariables.AllUserLittleATM_Config_ATMConfigFile);
        }
        #endregion
    }
}
